import { _decorator, Component, Node, Vec3, SkeletalAnimationState, AnimationClip, SkeletalAnimationComponent } from 'cc';
import { enemyBase } from './enemyBase';


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */

const { ccclass, property } = _decorator;
//玩家角色模型脚本
@ccclass('enemyModel')
export class enemyModel extends Component {


    @property(SkeletalAnimationComponent)
    public aniComPlayer: SkeletalAnimationComponent = null!;//动画播放组件


    public isAniPlaying: boolean = false;//当前动画是否正在播放



    private _aniType: string = "";//动画类型
    private _aniState: SkeletalAnimationState = null!;

    start() {

    }

    public stopAni() {
        this.aniComPlayer?.stop()
        this.isAniPlaying = false
    }

    /**
 * attack动画帧事件
 * @returns 
 */
    onFrameAttack() {
     

        this.node.parent.getComponent(enemyBase).attickCallback()
    }


    /**
 * 播放玩家动画
 *
 * @param {string} aniType 动画类型
 * @param {boolean} [isLoop=false] 是否循环
 * @param {Function} [callback] 回调函数
 * @param {number} [pos] 调用播放动画的位置，方便用于测试
 * @returns
 * @memberof Player
 */
    public playAni(aniType: string, isLoop: boolean = false, callback?: Function, speed?: number, pos?: number) {
        //  this.fun = null
        this._aniState = this.aniComPlayer?.getState(aniType) as SkeletalAnimationState;

        if (this._aniState && this._aniState.isPlaying) {

            return;
        }

        this._aniType = aniType;
        if (speed) {
            this._aniState.speed = speed
        } else {
            this._aniState.speed = 1
        }


        this.aniComPlayer?.play(aniType);

        this.isAniPlaying = true;

        if (this._aniState) {
            if (isLoop) {
                this._aniState.wrapMode = AnimationClip.WrapMode.Loop;
            } else {
                this._aniState.wrapMode = AnimationClip.WrapMode.Normal;
            }

            // switch (aniType) {
            //     case "attack":

            //         break;
            //     case "run":

            //         break;
            //     case "idle":

            //         break;
            //     default:

            //         break;
            // }
        }

        if (!isLoop) {
            this.aniComPlayer.once(SkeletalAnimationComponent.EventType.FINISHED, () => {
                //  console.log("组件里调用技能");
                // if (aniType == "attack" || aniType == "attack1" || aniType == "attack2") {
                //     if (callback != null) {
                //         this.fun = callback
                //         this.fun()
                //     }

                // }

                callback && callback();

                this.isAniPlaying = false;

            })
        }
    }


}